package 哈希;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author：小鹿爱吃鱼
 * @Date: 2025/5/3 17:00
 * @Description:
 */
public class 两数之和 {

    /**
     * 时间复杂度为2N 即 O(N)
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            map.put(nums[i], i);
        }
        for(int i = 0; i < nums.length; i++){
            int index = target - nums[i];
            if(map.containsKey(index) && map.get(index) != i){
                return new int[]{i, map.get(index)};
            }
        }

        return null;
    }

    public static void main(String[] args) {
        int[] nums = {3,3};
        int[] result = twoSum(nums, 6);
        System.out.println(Arrays.toString(result));
    }




}
